import { defineStore } from "pinia";

interface UserState {
  token: string | null;
  username: string | null;
  password: string | null;
}

export const useUserStore = defineStore("user", {
  state: (): UserState => ({
    token: localStorage.getItem("token"),
    username: null,
    password: null,
  }),
  actions: {
    login(username: string, password: string) {
      return new Promise<void>((resolve) => {
        // 模拟登录请求
        setTimeout(() => {
          this.token = "mock-token";
          this.username = username;
          this.password = password;
          localStorage.setItem("token", "mock-token");
          resolve();
        }, 500);
      });
    },
    logout() {
      this.token = null;
      this.username = null;
      this.password = null;
      localStorage.removeItem("token");
    },
  },
});
